﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;

public partial class Views_TrayMaterial : System.Web.UI.Page
{
    string lbl_ZoneID = "";//库区号 
    string lbl_LaneNo = ""; //巷道号
    string cid = "";        ////层
    int rid = 0;        //gridview行的编号
    int iLcol = 0;
    string PosID = "";

    DataSet ds = null;
    DataTable myTable = null;

    int iLineNo = 0;               //排
    int iLayNo = 0;           //层
    int colArea = 60;

    DbHelper access = new DbHelper();
    DbCommand cmd = null;

    Nova_Common.LoginLogic cLoginLogic = new LoginLogic();
    Nova_BLL.TBase_Position Nova_BLL_TBase_Position = new Nova_BLL.TBase_Position();
    Nova_BLL.TLaneDefine Nova_BLL_TLaneDefine = new Nova_BLL.TLaneDefine();
    Nova_BLL.TPosState Nova_BLL_TPostState = new Nova_BLL.TPosState();
    Nova_MOD.TBase_Position Nova_MOD_TBase_Position = new Nova_MOD.TBase_Position();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Nova_Common.LoginLogic.IsLogin();
            BindDllZone();//加载库区
            ddlbind();  //巷道项加载
            gvbind();

        }
    }


    protected void ddlbind()
    {
        ds = Nova_BLL_TLaneDefine.DataSetTLaneDefine();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            ddlNo_Lane.DataSource = ds;
            ddlNo_Lane.DataValueField = "No_Lane";
            ddlNo_Lane.DataTextField = "No_Lane_Text";
            ddlNo_Lane.DataBind();
        }
    }

    private void BindDllZone()
    {
        string sql = string.Format(@"select Zone_ID,Zone_Name from TBase_Zone where Structure <> 0 order by Zone_ID
            ");

        this.cmd = this.access.GetSqlStringCommond(sql);
        ds = this.access.ExecuteDataSet(this.cmd);

        ddlZone.DataTextField = "Zone_Name";
        ddlZone.DataValueField = "Zone_ID";
        ddlZone.DataSource = ds;
        ddlZone.DataBind();
        //ddlZone.Items.Insert(0, new ListItem("所有自动化库", "0"));
    }

    protected bool reInt(string strNum)
    {
        try
        {
            int var1 = Convert.ToInt32(strNum);
            return true;
        }
        catch
        {
            return false;
        }
    }


    protected void gvbind()
    {
        lbl_ZoneID = ddlZone.SelectedValue.ToString();
        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();
        int subArea = int.Parse(ddlNo_Col.SelectedValue);
        //xgw edit for 视图加载卡
        //myTable = Nova_BLL_TBase_Position.DTableTBase_PositionZoneID(lbl_ZoneID, lbl_LaneNo);
        myTable = Nova_BLL_TBase_Position.DTableTBase_PositionZoneIDPosImg(lbl_ZoneID, lbl_LaneNo,subArea);

        if (myTable != null && myTable.Rows.Count > 0)
        {
            GV.DataSource = myTable;
            GV.DataBind();
        }
    }

    protected void ddlNo_Lane_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
    }

    protected void ddlZone_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
    }


    protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        #region    
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Cells[1].Attributes["style"] = "width:30px;";
        //    cid = e.Row.Cells[1].Text.ToString().Substring(0, 1);  //层
        //    rid = e.Row.RowIndex + 1; //  视图的行号

        //    lbl_ZoneID = ddlZone.SelectedValue.ToString();
        //    lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
        //    iLineNo = Convert.ToInt32(e.Row.Cells[0].Text.Substring(0, 1));     //排
        //    //iLcol = e.Row.RowIndex;         //列
        //    iLayNo = Convert.ToInt32(e.Row.Cells[1].Text.Substring(0, 1));     //层
        //    DataRowView dv = (DataRowView)e.Row.DataItem;
        //    for (int i = 0; i < e.Row.Cells.Count; i++)
        //    {
        //        //列的处理
        //        if (i == 0)
        //        {
        //            iLcol = 1;
        //        }
        //        else if (i == 1)
        //        {
        //            iLcol = 1;
        //        }
        //        else
        //        {
        //            iLcol = i - 1;
        //        }

        //        if (i > 1)
        //        {

        //            PosID = lbl_ZoneID.PadLeft(2, '0') + lbl_LaneNo.PadLeft(2, '0') + iLineNo.ToString() + iLcol.ToString().PadLeft(3, '0') + iLayNo.ToString().PadLeft(2, '0') + "00";
        //            e.Row.Cells[i].Attributes["onclick"] = string.Format("window.showModalDialog('MetarPOP.aspx?PosID={0}', window, 'dialogWidth=1200px;dialogHeight=450px;scroll:no;resizable:no;status:0;');", PosID);

        //            e.Row.Cells[i].Text = Nova_BLL_TPostState.strPosImg(e.Row.Cells[i].Text.ToString());

        //        }

        //    }
        //}
        #endregion

        //每行绑定  点击行行触发SelectedIndexChanging事件（SelectedIndexChanging 不能自动触发）
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["style"] = "cursor:hand";

            #region   //点击行触发SelectedIndexChanging事件
            //PostBackOptions myPostBackOptions = new PostBackOptions(this);
            //myPostBackOptions.AutoPostBack = false;
            //myPostBackOptions.PerformValidation = false;
            //myPostBackOptions.RequiresJavaScriptProtocol = true; //加入javascript:头

            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.ToolTip = "Click to select row";

            String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex.ToString());
            e.Row.Attributes.Add("onclick", evt);
            #endregion
        }
    }

    /// <summary>
    /// 行改变加载行中的每个单元格的点击属性
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GV_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.GV.SelectedIndex = e.NewSelectedIndex;
        int index = e.NewSelectedIndex;
        string rid = (index + 1).ToString();
        //GV.Rows[index].BackColor = System.Drawing.Color.FromName("#CCCCCC"); //选中的行显示灰色
        //获取库区号
        lbl_ZoneID = ddlZone.SelectedValue.ToString();
        //获取巷道、排、列、层
        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
        string LineNo = GV.Rows[index].Cells[0].Text.Substring(0, 1);     //排

        string LayNo = GV.Rows[index].Cells[1].Text.Substring(0, GV.Rows[index].Cells[1].Text.Length - 1);     //层

        int sunArea = int.Parse(ddlNo_Col.SelectedValue);
        for (int i = 0; i < this.GV.Rows[index].Cells.Count; i++)
        {

            //列的数据获取处理  列从2开始
            //if (i < 2)
            //{
            //    iLcol = 2;
            //}
            //else
            //{
            //    iLcol = i;
            //}

            //if (i > 1)
            //{

            //    PosID = "10" + lbl_LaneNo.PadLeft(2, '0') + LineNo.ToString() + iLcol.ToString().PadLeft(3, '0') + LayNo.ToString().PadLeft(2, '0') + "00";
            //    this.GV.Rows[index].Cells[i].Attributes["onclick"] = string.Format("window.showModalDialog('MetarPOP.aspx?PosID={0}', window, 'dialogWidth=710px;dialogHeight=450px;scroll:no;resizable:no;status:0;');", PosID);


            //}

            //列的处理  列从1开始
            if(sunArea == 1)
            {
                if (i == 0)
                {
                    iLcol = 1;
                }
                else if (i == 1)
                {
                    iLcol = 1;
                }
                else
                {
                    iLcol = i - 1;
                }

            }
            else
            {
                if (i == 0)
                {
                    iLcol = colArea + 1;
                }
                else if (i == 1)
                {
                    iLcol = colArea + 1;
                }
                else
                {
                    iLcol = colArea + (i - 1);
                }

            }

            if (i > 1)
            {
                PosID = lbl_ZoneID.PadLeft(2, '0') + lbl_LaneNo.PadLeft(2, '0') + LineNo.ToString() + iLcol.ToString().PadLeft(3, '0') + LayNo.ToString().PadLeft(2, '0') + "00";
                string str = string.Format("javascript:window.open('MetarPOP.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&PosID={0}', window, 'Height=450px,Width=1100px,top=200, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');document.URL=location.href;void(0);",
                PosID);
                this.GV.Rows[index].Cells[i].Attributes["onclick"]= str;
            }

        }
    }


    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        gvbind();
    }

    protected void ddlNo_Col_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
    }
}
